import json
import os
from collections import OrderedDict
import pandas as pd

def merge_json_files(input_dir, output_file):
    """
    合并目录下所有JSON文件到一个文件中，格式为 {filename: content}
    :param input_dir: 包含JSON文件的目录
    :param output_file: 输出文件路径
    """
    merged_data = OrderedDict()
    
    # 遍历目录下的所有JSON文件
    for filename in os.listdir(input_dir):
        if filename.endswith('.json'):
            filepath = os.path.join(input_dir, filename)
            try:
                with open(filepath, 'r', encoding='utf-8') as f:
                    # 使用文件名（不带扩展名）作为key
                    key = os.path.splitext(filename)[0]
                    merged_data[key] = json.load(f)
            except Exception as e:
                print(f"处理文件 {filename} 时出错: {e}")
    
    # 保存合并后的数据
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(merged_data, f, ensure_ascii=False, indent=4)
    
    print(f"已成功合并文件到 {output_file}")

def excel_to_csv():
    # 读取Excel（可添加chunksize分块读取大文件）
    df = pd.read_excel("../final_basic_tables/A075. PCdata基表-3-参考文献表（20250604R1）-1版.xlsx", 
                    sheet_name="文献总表",
                    header=0,
                    skiprows=[1],
                    engine='openpyxl')

    # 保存为CSV（UTF-8编码避免中文乱码）
    df.to_csv("A075_converted.csv", index=False, encoding='utf_8_sig')

    print("转换完成！CSV文件已保存为 A075_converted.csv")
if __name__ == "__main__":
    input_directory = './liter_files'  
    output_filename = './literature_prop.json'
    excel_to_csv()
    # merge_json_files(input_directory, output_filename)
    
    # # 验证文件可正确读取
    # with open(output_filename, 'r', encoding='utf-8') as f:
    #     data = json.load(f)
    # # 检查 key 是否存在
    # if "liter_status_data" in data:
    #     # 遍历 liter_status_data 列表
    #     for item in data["liter_status_data"]:
    #         print(f"英文名: {item['name']}, 中文名: {item['zh_name']}")
    # else:
    #     print("错误: liter_status_data不存在")